﻿if (typeof ($) != "undefined") {
    $(document).ready(function () {
        var now = new Date();
        y = now.getFullYear();
        m = now.getMonth() + 1;
        d = now.getDate();
        m = m < 10 ? "0" + m : m;
        d = d < 10 ? "0" + d : d;
        var myDate = y + "-" + m + "-" + d;
        instance[0].Status = "1";
        var fun = getPar1("fun");
        if (fun == "add") { //添加岗位需求
            $("#ReportDate").val(myDate); //提报日期为当前日期
            $("#applyDate").val(myDate); //申请日期为当前日期
            //获取提报人employeeId
            YAHOO.util.Connect.asyncRequest('POST', "../EHR/GetCurrentUser.aspx", {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str != "-1") {
                            var token = str.split('_');
                            $("#applyMan").val(token[1]);
                            var deptId = token[2];
                            GetPostions(deptId);
                            instance[0].ApplyEmployeeID = token[0];
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
        else if (fun == "edit") { //修改          
            YAHOO.util.Connect.asyncRequest('POST', "../EHR/GetCurrentUser.aspx", {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str != "-1") {
                            var token = str.split('_');
                            var deptId = token[2];
                            GetPostions(deptId);
                            var applyMan = getPar(decodeURI(location.href), "applyMan"); //申请人
                            var applyDate = getPar(decodeURI(location.href), "applyDate"); //申请日期
                            $("#applyMan").val(applyMan);
                            $("#applyDate").val(applyDate);
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
        else if (fun == "audit") { //审核
            //获取审核人employeeId
            YAHOO.util.Connect.asyncRequest('POST', "../EHR/GetCurrentUser.aspx", {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str != "-1") {
                            var applyMan = getPar(decodeURI(location.href), "applyMan"); //申请人
                            var applyDate = getPar(decodeURI(location.href), "applyDate"); //申请日期
                            var postName = getPar(decodeURI(location.href), "postName"); //需求岗位
                            $("#applyMan").val(applyMan);
                            $("#applyDate").val(applyDate);
                            $("#table").append(" <tr><td class='td_text'>审核人：</td><td class='td_value'><input id='auditMan' name='auditMan' readonly='readonly' style='background-color: whitesmoke; width: 152px' type='text' /></td><td class='td_value'>审核日期：</td><td class='td_value'><input id='auditDate' name='auditDate' readonly='readonly' style='background-color: whitesmoke; width: 152px' type='text' /></td></tr><tr><td class='td_text'>审核结果：</td><td class='td_value'><select id='auditResult' name='auditResult' style='width: 156px' title=''></select><span style='color: #ff0000'>*</span></td><td class='td_value'> 允招人数：</td><td class='td_value'><input datafields='AvailableNumber' id='AvailableNumber' name='AvailableNumber' style='width: 152px' type='text' /><span style='color: #ff0000'>*</span></td></tr> <tr><td class='td_text'>审核说明：</td><td class='td_value' colspan='3'><textarea cols='30' datafields='AuditRemark' id='AuditRemark' name='AuditRemark' rows='4'></textarea></td></tr>");
                            var token = str.split('_');
                            //                            var deptId = token[2];
                            //                            GetPostions(deptId);
                            $("#ApplyPostID").append("<option value='" + instance[0].ApplyPostID + "' title='" + postName + "'>" + postName + "</option>");
                            $("#ApplyPostID").attr("disabled", true);
                            $("#ApplyPostID").css("background", "whitesmoke");
                            $("#auditMan").val(token[1]);
                            $("#auditDate").val(myDate);
                            $("#auditResult").append("<option value='2'>通过</option>");
                            $("#auditResult").append("<option value='3'>不通过</option>");
                            $("#Status").val("2");
                            $("#auditResult").change(function () {
                                $("#Status").val($("#auditResult").val());
                            });
                            //设置只可读，并且背景色为烟灰色                   
                            $("#ApplyNumber").attr("readonly", "readonly");
                            $("#ApplyNumber").css("background", "whitesmoke");
                            $("#EmploymentDate").attr("readonly", "readonly");
                            $("#EmploymentDate").css("background", "whitesmoke");
                            $("#PostRequirement").attr("readonly", "readonly");
                            $("#PostRequirement").css("background", "whitesmoke");
                            $("#Remark").attr("readonly", "readonly");
                            $("#Remark").css("background", "whitesmoke");
                            $("#AvailableNumber").val($("#ApplyNumber").val()); //允招人数默认为申请人数
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
        else if (fun == "view") {
            var applyMan = getPar(decodeURI(location.href), "applyMan"); //申请人
            var applyDate = getPar(decodeURI(location.href), "applyDate"); //申请日期
            var postName = getPar(decodeURI(location.href), "postName"); //需求岗位

            $("#applyMan").val(applyMan);
            $("#applyDate").val(applyDate);
            $("#ApplyPostID").append("<option>" + postName + "</option>");
            $("#ApplyPostID").attr("disabled", true);
            $("#ApplyPostID").css("background", "whitesmoke");
            //设置只可读，并且背景色为烟灰色                   
            $("#ApplyNumber").attr("readonly", "readonly");
            $("#ApplyNumber").css("background", "whitesmoke");
            $("#EmploymentDate").attr("readonly", "readonly");
            $("#EmploymentDate").css("background", "whitesmoke");
            $("#PostRequirement").attr("readonly", "readonly");
            $("#PostRequirement").css("background", "whitesmoke");
            $("#Remark").attr("readonly", "readonly");
            $("#Remark").css("background", "whitesmoke");
            $("#AvailableNumber").val($("#ApplyNumber").val()); //允招人数默认为申请人数
            var btn = parent.document.getElementById("yui-gen7-button");
            $(btn).hide();
        }
    });
}

//获取部门下的岗位
function GetPostions(deptID) {
    $("#ApplyPostID").find('option').remove();
    $("#tishixinxi").text("");
    //var url = "../Extend/AjaxData/jsonlist.aspx?entity=DatPost&condition=DeptID='" + deptID + "'";
    var url2 = encodeURI("../Extend/AjaxData/jsonlist.aspx?entity=DatPost&condition=DeptID in (select DeptID from DatDepartment where DeptPath like (select DeptPath") + "%2B" + encodeURI(" '%' from DatDepartment where DeptID='" + deptID + "'))");
    $.get(url2, function (d, s) {
        var list = eval("(" + d + ")");
        if (list.total == 0) {
            $("#tishixinxi").text("该部门无岗位信息，请先维护部门岗位信息！");
        }
        else {
            for (var o in list.rows) {
                var v = list.rows[o].PostID;
                var t = list.rows[o].PostName;
                if (typeof (t) != "undefined") {
                    $("#ApplyPostID").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                }
            }
            var fun = getPar1("fun");
            if (fun == "edit") $("#ApplyPostID").attr("value", instance[0].ApplyPostID); //岗位
//            if (fun == "edit" || fun == "audit") { //修改或审核时
//            $("#ApplyPostID").attr("value", instance[0].ApplyPostID); //岗位
//                if (fun == "audit") {
//                    $("#ApplyPostID").attr("disabled", true);
//                    $("#ApplyPostID").css("background", "whitesmoke");
//                }
//            }
        }
    });
}

//数据验证
var fnCheckData = function () {
    var fun = getPar1("fun");
    if (fun != "audit") {  //修改或添加时
        if ($("#ApplyPostID").val() == "") {
            return "岗位名称不能为空！";
        }
        if (/^([1-9])(\d*)$/.test($("#ApplyNumber").val()) == false) {
            return "需求人数需要输入非零数字!";
        }
        if ($("#PostRequirement").val() == "") {
            return "岗位要求不能为空！";
        }
        //验证截止日期
        var now = new Date();
        y = now.getFullYear();
        m = now.getMonth() + 1;
        d = now.getDate();
        m = m < 10 ? "0" + m : m;
        d = d < 10 ? "0" + d : d;
        var begin = y + "-" + m + "-" + d;
        var end = $("#EmploymentDate").val();
        if (end == "") {
            return "用工日期不能为空！";
        }
        if (begin >= end) {
            return "用工日期小于当前日期，不允许!";
        }
    }
    else { //审核时
        if (/^([1-9])(\d*)$/.test($("#AvailableNumber").val()) == false) {
            return "允许招聘人数需要输入非零数字";
        }
    }
}